﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace EasyFramework.Web
{
    public class URLHelper
    {
        /// <summary>
        /// 版权：广联达软件股份有限公司
        /// 创建：战东耀, 2012-04-13
        /// 说明: 添加参数至URL，copy from We7cms
        /// 修改: 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="paramName"></param>
        /// <param name="paramValue"></param>
        /// <returns></returns>
        public static string AddParamToUrl(string url, string paramName, string paramValue)
        {
            string regexstr, regexstr2, fileNameExt;
            fileNameExt = "html";
            regexstr = "(/?)$";
            regexstr2 = string.Format(@"(\.(aspx|{0}))$", fileNameExt);
            if (Regex.IsMatch(url, regexstr, RegexOptions.IgnoreCase) && !Regex.IsMatch(url, regexstr2, RegexOptions.IgnoreCase) && url.IndexOf("?") < 0)
            {
                if (!url.EndsWith("/"))
                {
                    url += "/";
                }
                url += "default." + fileNameExt;
            }
            regexstr = string.Format(@"{0}=\s*(.*?)&", paramName);
            if (Regex.IsMatch(url, regexstr, RegexOptions.IgnoreCase))
            {
                return Regex.Replace(url, regexstr, paramName + "=" + paramValue + "&", RegexOptions.IgnoreCase);
            }
            regexstr = string.Format(@"{0}=\s*(.*?)$", paramName);
            if (Regex.IsMatch(url, regexstr, RegexOptions.IgnoreCase))
            {
                return Regex.Replace(url, regexstr, paramName + "=" + paramValue, RegexOptions.IgnoreCase);
            }
            if (url.IndexOf("?") > -1)
            {
                return url + "&" + paramName + "=" + paramValue;
            }
            return url + "?" + paramName + "=" + paramValue;
        }

        /// <summary>
        /// 版权：广联达软件股份有限公司
        /// 创建：战东耀, 2012-04-13
        /// 说明: 移除参数至URL，copy from We7cms
        /// 修改: 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="paramName"></param>
        /// <returns></returns>
        public static string RemoveParamFromUrl(string url, string paramName)
        {
            string regexstr;
            //regexstr = string.Format(@"{0}=\s*(.*)&", paramName);
            regexstr = string.Format(@"{0}=[^&]*&", paramName);
            if (Regex.IsMatch(url, regexstr, RegexOptions.IgnoreCase))
            {
                return Regex.Replace(url, regexstr, string.Empty, RegexOptions.IgnoreCase);
            }
            //regexstr = string.Format(@"([&]|[?]){0}=\s*(.*)$", paramName);
            regexstr = string.Format(@"([&]|[?]){0}=[^&]*$", paramName);
            if (Regex.IsMatch(url, regexstr, RegexOptions.IgnoreCase))
            {
                return Regex.Replace(url, regexstr, string.Empty, RegexOptions.IgnoreCase);

            }

            return url;
        }

        /// <summary>
        /// 版权：广联达软件股份有限公司
        /// 创建：战东耀, 2012-04-13
        /// 说明: 获取指定参数的值，copy from We7cms
        /// 修改: 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="paramName"></param>
        /// <returns></returns>
        public static string GetParamValueFromUrl(string url, string paramName)
        {
            string qs = paramName + "=";
            if (url.Length > 0)
            {
                int begin = url.IndexOf(qs);
                if (begin != -1)
                {
                    if (url.Substring(begin - 1, 1) == "?" || url.Substring(begin - 1, 1) == "&")
                    {
                        begin += qs.Length;
                        int end = url.IndexOf("&", begin);
                        if (end == -1) end = url.Length;
                        return (url.Substring(begin, end - begin));
                    }
                }
            }
            return "";
        }
    }
}
